/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmRepeat.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "models/SCAlarmRepeat.h"
#include "SCDatabase.h"
#include "SCTranslator.h"

SCAlarmRepeat::SCAlarmRepeat()
{
	this->id = SCAlarmRepeat::NEW_OBJECT;
	this->SetDefaultValues();
}

SCAlarmRepeat::SCAlarmRepeat(int id)
{
	this->id = id;
	this->SetDefaultValues();
}

SCAlarmRepeat::~SCAlarmRepeat()
{
	// TODO Auto-generated destructor stub
}

result SCAlarmRepeat::Construct()
{
	return E_SUCCESS;
}


bool
SCAlarmRepeat::GetMonday()
{
	return this->monday;
}

void
SCAlarmRepeat::SetMonday(bool active)
{
	this->monday = active;
}

bool
SCAlarmRepeat::GetTuesday()
{
	return this->tuesday;
}

void
SCAlarmRepeat::SetTuesday(bool active)
{
	this->tuesday = active;
}

bool
SCAlarmRepeat::GetWednesday()
{
	return this->wednesday;
}

void
SCAlarmRepeat::SetWednesday(bool active)
{
	this->wednesday = active;
}

bool
SCAlarmRepeat::GetThursday()
{
	return this->thursday;
}

void
SCAlarmRepeat::SetThursday(bool active)
{
	this->thursday = active;
}

bool
SCAlarmRepeat::GetFriday()
{
	return this->friday;
}

void
SCAlarmRepeat::SetFriday(bool active)
{
	this->friday = active;
}

bool
SCAlarmRepeat::GetSaturday()
{
	return this->saturday;
}

void
SCAlarmRepeat::SetSaturday(bool active)
{
	this->saturday = active;
}

bool
SCAlarmRepeat::GetSunday()
{
	return this->sunday;
}

void
SCAlarmRepeat::SetSunday(bool active)
{
	this->sunday = active;
}

bool
SCAlarmRepeat::Load()
{
	SCDatabase::LoadAlarmRepeat(this);
	return true;
}

bool
SCAlarmRepeat::Save()
{
	SCDatabase::SaveAlarmRepeat(this);
	return true;
}

void
SCAlarmRepeat::ClearAllDays()
{
	this->SetMonday(false);
	this->SetTuesday(false);
	this->SetWednesday(false);
	this->SetThursday(false);
	this->SetFriday(false);
	this->SetSaturday(false);
	this->SetSunday(false);
}

void
SCAlarmRepeat::SetDefaultValues()
{
	this->SetOnlyOnce();
}

void
SCAlarmRepeat::SetOnlyOnce()
{
	this->SetMonday(false);
	this->SetTuesday(false);
	this->SetWednesday(false);
	this->SetThursday(false);
	this->SetFriday(false);
	this->SetSaturday(false);
	this->SetSunday(false);
}

void
SCAlarmRepeat::SetOnlyWeekend()
{
	this->ClearAllDays();
	this->SetSaturday(true);
	this->SetSunday(true);
}

void
SCAlarmRepeat::SetOnlyWeekDays()
{
	this->ClearAllDays();
	this->SetMonday(true);
	this->SetTuesday(true);
	this->SetWednesday(true);
	this->SetThursday(true);
	this->SetFriday(true);
}

void
SCAlarmRepeat::SetEveryDay()
{
	this->ClearAllDays();
	this->SetMonday(true);
	this->SetTuesday(true);
	this->SetWednesday(true);
	this->SetThursday(true);
	this->SetFriday(true);
	this->SetSaturday(true);
	this->SetSunday(true);
}

String
SCAlarmRepeat::GetActiveDaysStr()
{
	String days;

	if(this->monday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_MONDAY"));
	if(this->tuesday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_TUESDAY"));
	if(this->wednesday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_WEDNESDAY"));
	if(this->thursday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_THURSDAY"));
	if(this->friday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_FRIDAY"));
	if(this->saturday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_SATURDAY"));
	if(this->sunday)
		days.Append(SCTranslator::GetText("IDS_ALARMSREPEAT_SUNDAY"));

	days.SubString(0, days.GetLength() - 1, days);

	return days;
}

HashMapT<String*, bool>*
SCAlarmRepeat::GetAllDays()
{
	HashMapT<String*, bool>* pHashMap = new HashMapT<String*, bool>();
	pHashMap->Construct();

	if(pHashMap != NULL)
	{
		pHashMap->Add(new String("monday"), this->monday);
		pHashMap->Add(new String("tuesday"), this->tuesday);
		pHashMap->Add(new String("wednesday"), this->wednesday);
		pHashMap->Add(new String("thursday"), this->thursday);
		pHashMap->Add(new String("friday"), this->friday);
		pHashMap->Add(new String("saturday"), this->saturday);
		pHashMap->Add(new String("sunday"), this->sunday);
	} else {
		AppLog("Chyba alokace HashMapT.");
	}

	return pHashMap;
}

HashMapT<String*, bool>*
SCAlarmRepeat::GetActiveDays()
{
	HashMapT<String*, bool>* pHashMapAll = this->GetAllDays();
	HashMapT<String*, bool>* pHashMapActive = new HashMapT<String*, bool>();
	pHashMapActive->Construct();
	IMapEnumeratorT<String*, bool>*  pMapEnum = pHashMapAll->GetMapEnumeratorN();
	String *key;
	bool value;

	while(pMapEnum->MoveNext() == E_SUCCESS)
	{
		pMapEnum->GetValue(value);
		if(value == true)
		{
			pMapEnum->GetKey(key);
			pHashMapActive->Add(key, value);
		}
	}

	//dealloc
	delete pMapEnum;

	return pHashMapActive;
}

bool
SCAlarmRepeat::IsJustWeekend()
{
	if((this->saturday == true) && (this->sunday == true)
			&& (this->monday == false) && (this->tuesday == false) && (this->wednesday == false)
			&& (this->thursday == false) && (this->friday == false))
	{
		return true;
	} else {
		return false;
	}
}

bool
SCAlarmRepeat::IsJustWeekdays()
{
	if((this->monday == true) && (this->tuesday == true) && (this->wednesday == true)
			&& (this->thursday == true) && (this->friday == true)
			&& (this->saturday == false) && (this->sunday == false))
	{
		return true;
	} else {
		return false;
	}
}

bool
SCAlarmRepeat::IsJustOnlyOnce()
{
	if(this->sunday == false && this->saturday == false
			&& this->monday == false && this->tuesday == false
			&& this->wednesday == false && this->thursday == false
			&& this->friday == false )
	{
		return true;
	} else {
		return false;
	}
}

bool
SCAlarmRepeat::IsEveryDays()
{
	if(this->sunday == true && this->saturday == true
			&& this->monday == true && this->tuesday == true
			&& this->wednesday == true && this->thursday == true
			&& this->friday == true )
	{
		return true;
	} else {
		return false;
	}
}

bool
SCAlarmRepeat::IsActiveDay(int day)
{
	bool retVal = false;

	switch(day)
	{
		case 1:
			if(this->sunday)
				retVal = true;
			break;
		case 2:
			if(this->monday)
				retVal = true;
			break;
		case 3:
			if(this->tuesday)
				retVal = true;
			break;
		case 4:
			if(this->wednesday)
				retVal = true;
			break;
		case 5:
			if(this->thursday)
				retVal = true;
			break;
		case 6:
			if(this->friday)
				retVal = true;
			break;
		case 7:
			if(this->saturday)
				retVal = true;
			break;
	}

	return retVal;
}

String
SCAlarmRepeat::GetRepeatStateString()
{
	if(this->IsJustOnlyOnce())
	{
		return SCTranslator::GetText("IDS_ALARMSREPEAT_ONLYONCE");
	} else if(this->IsEveryDays()) {
		return SCTranslator::GetText("IDS_ALARMSREPEAT_EVERYDAY");
	} else if(this->IsJustWeekdays()) {
		return SCTranslator::GetText("IDS_ALARMSREPEAT_WEEKDAYS");
	} else if(this->IsJustWeekend()) {
		return SCTranslator::GetText("IDS_ALARMSREPEAT_WEEKEND");
	} else {
		return this->GetActiveDaysStr();
	}
}

